package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu  menu){
        try{
            menuService.add(menu);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    //分页查询菜单
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }

    //根据id删除菜单
    @RequestMapping("/delete")
    public   Result   delete(Integer  id){
        try{
            menuService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }
    @Reference
    private UserService userService;
    @Reference
    private RoleService roleService;

    @RequestMapping("/showMenuByPermission")
    public Result showMenuByPermission(String username) {
        try {
            User user = userService.findByUsername(username);
            Set<Role> roles = roleService.findByUserId(user.getId());
            List<Map> list = new ArrayList<>();
            for (Role role : roles) {
                List<Menu> menus = menuService.showMenuByPermission(role.getId());
                for (Menu menu : menus) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("path",menu.getPath());
                    map.put("title",menu.getTitle());
                    map.put("icon",menu.getIcon());
                    List<Menu> childrenMenu = menuService.findChildrenById(menu.getId());
                    List<Map> childList = new ArrayList<>();
                    if(childrenMenu != null && childrenMenu.size() > 0){
                        for (Menu childrenMenu1 : childrenMenu) {
                            Map<String,Object> childMap = new HashMap<>();
                            childMap.put("path",childrenMenu1.getPath());
                            childMap.put("title",childrenMenu1.getTitle());
                            childMap.put("linkUrl",childrenMenu1.getLinkUrl());
                            childMap.put("children",new ArrayList<>());
                            childList.add(childMap);
                        }
                        map.put("children",childList);
                    }
                    list.add(map);
                }
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

}
